package utils

import (
	"crypto/tls"
	"fmt"
	"net/smtp"
)

/*
	   // SMTP 配置
		host := "smtp.example.com"       // SMTP 服务器地址
		port := "465"                    // SMTP 服务器端口（SSL 通常为 465）
		username := "your_email@example.com" // 发件人邮箱
		password := "your_password"      // 发件人邮箱密码

		// 邮件内容
		to := "recipient@example.com" // 收件人邮箱
		subject := "Test Email"       // 邮件主题
		body := "Hello, this is a test email sent via SSL SMTP in Go!" // 邮件正文

		// 发送邮件
		err := SendEmailWithSSL(host, port, username, password, to, subject, body)
		if err != nil {
			fmt.Println("Error sending email:", err)
		} else {
			fmt.Println("Email sent successfully!")
		}
*/
func SendEmailWithSSL(host, port, username, password, to, subject, body string) error {
	// 邮件服务器地址
	serverAddr := host + ":" + port

	// 创建邮件内容
	message := []byte("Subject: " + subject + "\r\n" +
		"From: " + username + "\r\n" +
		"To: " + to + "\r\n" +
		"Content-Type: text/plain; charset=UTF-8\r\n\r\n" +
		body)

	// 设置 TLS 配置
	tlsConfig := &tls.Config{
		InsecureSkipVerify: false, // 是否跳过证书验证（生产环境建议设置为 false，并使用有效证书）
		ServerName:         host,  // 服务器名称
	}

	// 建立 TLS 连接
	conn, err := tls.Dial("tcp", serverAddr, tlsConfig)
	if err != nil {
		return fmt.Errorf("failed to connect to SMTP server: %v", err)
	}
	defer conn.Close()

	// 创建 SMTP 客户端
	client, err := smtp.NewClient(conn, host)
	if err != nil {
		return fmt.Errorf("failed to create SMTP client: %v", err)
	}
	defer client.Close()

	// 验证身份
	auth := smtp.PlainAuth("", username, password, host)
	if err := client.Auth(auth); err != nil {
		return fmt.Errorf("failed to authenticate: %v", err)
	}

	// 设置发件人和收件人
	if err := client.Mail(username); err != nil {
		return fmt.Errorf("failed to set sender: %v", err)
	}
	if err := client.Rcpt(to); err != nil {
		return fmt.Errorf("failed to set recipient: %v", err)
	}

	// 发送邮件数据
	writer, err := client.Data()
	if err != nil {
		return fmt.Errorf("failed to send email data: %v", err)
	}
	_, err = writer.Write(message)
	if err != nil {
		return fmt.Errorf("failed to write message: %v", err)
	}
	err = writer.Close()
	if err != nil {
		return fmt.Errorf("failed to close writer: %v", err)
	}

	return nil
}

/*
// SMTP 配置

	host := "smtp.example.com"       // SMTP 服务器地址
	port := "25"                     // SMTP 服务器端口（通常为 25 或 587）
	username := "your_email@example.com" // 发件人邮箱
	password := "your_password"      // 发件人邮箱密码

	// 邮件内容
	to := "recipient@example.com" // 收件人邮箱
	subject := "Test Email"       // 邮件主题
	body := "Hello, this is a test email sent without SSL in Go!" // 邮件正文

	// 发送邮件
	err := SendEmailWithoutSSL(host, port, username, password, to, subject, body)
	if err != nil {
		fmt.Println("Error sending email:", err)
	} else {
		fmt.Println("Email sent successfully!")
	}
*/
func SendEmailWithoutSSL(host, port, username, password, to, subject, body string) error {
	// 邮件服务器地址
	serverAddr := host + ":" + port

	// 创建邮件内容
	message := []byte("Subject: " + subject + "\r\n" +
		"From: " + username + "\r\n" +
		"To: " + to + "\r\n" +
		"Content-Type: text/plain; charset=UTF-8\r\n\r\n" +
		body)

	// 设置身份验证
	auth := smtp.PlainAuth("", username, password, host)

	// 发送邮件
	err := smtp.SendMail(serverAddr, auth, username, []string{to}, message)
	if err != nil {
		return fmt.Errorf("failed to send email: %v", err)
	}

	return nil
}
